<?php declare(strict_types=1);
/**
 * @author      xianganyall <xianganyall@gmail.com>
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Common\Ctl;

use Srv\Apps\Proto\Enum\BannerOpenTypeEnum;
use Srv\Libs\Common\CommJson;
use Srv\Libs\Common\CommValid;
use Srv\Libs\Frame\ControllerAbstract;

final class ToUrlCtl extends ControllerAbstract
{
    public static string $protocol                  = 'xctv://';                // 协议头[xxx://]
    public const U_HOME                             = 'home';                   // 首页
    public const U_WEBVIEW                          = 'webview';                // 打开网页`{"url":"https://","hide_nav_bar":0,"browser_open":0}`url:网页地址,hide_nav_bar:[0-不隐藏,1-隐藏导航条],browser_open:[0-INAPP,1-浏览器]
    public const U_ABOUT                            = 'about';                  // 关于我们
    public const U_CONTACT                          = 'contact';                // 联系客服
    public const U_PRIVACY_POLICY                   = 'privacy/policy';         // 隐私协议
    public const U_REGISTER_POLICY                  = 'register/policy';        // 注册协议
    public const U_ME                               = 'me';	                    // 我的主页
    public const U_ME_SECURITY                      = 'me/security';            // 账号安全
    public const U_ME_LOGOUT                        = 'me/logout';              // 账号注销
    public const U_ME_WALLET                        = 'me/wallet';              // 我的钱包
    public const U_SHOP                             = 'shop';                   // 商品商店
    public const U_ORDER_LIST                       = 'order/list';             // 订单列表
    public const U_COIN_GIVE_LIST                   = 'coin/give/list';         // Coin赠送列表
    public const U_ME_BUY_LIST                      = 'me/buy/list';            // 已购列表
    public const U_UNLOCK_CONF_LIST                 = 'unlock/conf/list';       // 解锁设置列表
    public const U_NAV_RECOMMEND                    = 'nav/recommend';          // 推荐-导航
    public const U_NAV_CHASING                      = 'nav/chasing';            // 追剧-导航
    public const U_NAV_PLAY_LIST                    = 'nav/play/list';          // 播放记录-导航
    public const U_NAV_THEATER                      = 'nav/theater';            // 剧场-导航
    public const U_SKIT_PLAY                        = 'skit/play';              // 短剧播放`{"skioKey":"","skssKey":"","continuePlay":0}`skioKey:短剧Key,skssKey:剧集Key[可选],continuePlay:继续播放[0-否,1-是][可选]
    public const U_SKIT_WATCH_PLAY                  = 'skit/watch/play';        // 观看继续播放

    /**
     * @return static
     * tag
     */
    final public static function tag():self
    {
        return self::tagGet();
    }

    /**
     * @param string $pageName
     * @param array $paramData
     * @return string
     * genToUrl
     */
    final public static function genToUrl(string $pageName, array $paramData = []):string
    {
        return self::$protocol.$pageName.(count($paramData) > 0 ? '?_json_='.urlencode(CommJson::encodeArray($paramData)) : '');
    }

    /**
     * @param string $toUrl
     * @param int $openType
     * @param int $hideNavBar
     * @return string
     * genAutoToUrl
     */
    final public static function genAutoToUrl(string $toUrl, int $openType, int $hideNavBar):string
    {
        if(CommValid::isHttps($toUrl) || CommValid::isHttp($toUrl)){
            if($openType === BannerOpenTypeEnum::BANNER_OPEN_TYPE_APP && $hideNavBar === 0) return $toUrl;
            return self::genToUrl(self::U_WEBVIEW, ['url' => $toUrl, 'browser_open' => $openType === 1 ? 1 : 0, 'hide_nav_bar' => $hideNavBar === 1 ? 1 : 0]);
        }else{
            return $toUrl;
        }
    }
}